<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="queryForm.userName" clearable placeholder="请输入用户名" />
          </el-form-item>

          <el-form-item>
            <el-select v-model="queryForm.activeGroup" placeholder="请选择小组">
              <el-option v-for="item in groupList" :key="item.id" :label="item.groupName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table border :height="height" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column label="用户ID" prop="id" show-overflow-tooltip width="100" />
      <el-table-column label="用户头像" show-overflow-tooltip width="100">
        <template #default="{ row }">
          <el-image fit="contain" :src="$baseImageUrl + row.userAvator" />
        </template>
      </el-table-column>

      <el-table-column label="用户名" prop="userName" show-overflow-tooltip width="150" />
      <el-table-column label="性别" prop="sex" show-overflow-tooltip width="150" />
      <el-table-column label="手机号" prop="userTel" show-overflow-tooltip />

      <el-table-column label="用户积分小组" show-overflow-tooltip prop="departments" />
      <el-table-column label="注册时间" prop="createTime" />

      <el-table-column align="center" label="用户当前积分" show-overflow-tooltip prop="score" width="150" />

      <el-table-column label="是否为评审" prop="isAdmin" width="200">
        <template #default="{ row }">
          <el-switch
            v-model="row.isAdmin"
            :active-value="1"
            :inactive-value="0"
            active-text="设为评审"
            inactive-text="取消资格"
            active-color="#13ce66"
            inactive-color="#ffa91b"
            @change="changeAdmin(row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" show-overflow-tooltip width="200">
        <template #default="{ row }">
          <el-button size="mini" type="info" @click="scoreInfo(row)">积分详情</el-button>
          <el-button size="mini" type="warning" @click="handleEdit(row)">修改积分</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { SelActivitygroup, SelMiniUserList, DelMiniUser, SysSetUserAdmin } from '@/api/userManagement'
  import Edit from './components/UserManagementEdit'

  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        groupList: [{ id: 0, groupName: '全部' }],
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          userName: '',
          dpId: '',
          activeGroup: 0,
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    async created() {
      let { data } = await SelActivitygroup({
        groupName: '',
      })
      this.groupList = [...this.groupList, ...data]

      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      async changeAdmin(row) {
        let { msg } = await SysSetUserAdmin({ userId: row.id, isAdmin: row.isAdmin })
        this.$baseMessage(msg, 'success')
        this.fetchData()
      },
      //积分详情
      scoreInfo(user) {
        this.$router.push({
          path: 'scoreList',
          query: user,
        })
      },
      //编辑
      handleEdit(row) {
        this.$refs['edit'].showEdit(row, this.groupList)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await DelMiniUser({ userid: row.id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      department(id) {
        if (id != 0) {
          let dpment = this.groupList.find((item) => item.id === id)
          return dpment.groupName
        } else {
          return '未设置小组'
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        let box = Object.assign({}, this.queryForm)
        box.dpId = box.dpId === '' ? 0 : box.dpId
        SelMiniUserList(box).then((res1) => {
          res1.data.records.forEach((element) => {
            element.departments = this.department(element.activityGroupId)
          })
          this.list = res1.data.records
          this.total = res1.data.total
        })

        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
